import requests
import xlwt
import json
from urllib import  request
xls=xlwt.Workbook()
sheet=xls.add_sheet('电影数据')
title=['电影名称','评分','主演','电影类型','上映时间', '总时间']
for i in range(len(title)):
    sheet.write(0, i, title[i])
    # 信息
url_string=r"https://douban.uieee.com/v2/movie/in_theaters"
response=requests.get(url_string)

obj=response.json()
lis=obj['subjects']

for i in range(len(lis)):
    pianming=lis[i]['title']
    sheet.write(i+1,0,pianming)
    pingfen=lis[i]['rating']['average']
    sheet.write(i+1,1,pingfen)
    name = []
    for j in range(len(lis[i]['casts'])):
        name.append(lis[i]['casts'][j]['name'])
    sheet.write(i+1,2,','.join(name))
    leixing=lis[i]['genres']
    sheet.write(i+1,3,leixing)
    shijian=lis[i]['pubdates']
    sheet.write(i+1,4,shijian)
    zongshijian=lis[i]['durations']
    sheet.write(i+1,5,zongshijian)

xls.save('电影数据.xls')


url_string=r"https://douban.uieee.com/v2/movie/in_theaters"
with request.urlopen(url_string) as f:
    if f.status==200:

        data=f.read().decode('utf-8')
        data=json.loads(data)
        lis = data['subjects']

        for i in range(len(lis)):
            pianming = lis[i]['title']
            sheet.write(i + 1, 0, pianming)
            pingfen = lis[i]['rating']['average']
            sheet.write(i + 1, 1, pingfen)
            name = []
            for j in range(len(lis[i]['casts'])):
                name.append(lis[i]['casts'][j]['name'])
            sheet.write(i + 1, 2, ','.join(name))
            leixing = lis[i]['genres']
            sheet.write(i + 1, 3, leixing)
            shijian = lis[i]['pubdates']
            sheet.write(i + 1, 4, shijian)
            zongshijian = lis[i]['durations']
            sheet.write(i + 1, 5, zongshijian)

        xls.save('电影数据.xls')